package com.dzt.androidframework.network.mobapi.cook;

import android.databinding.BindingAdapter;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

/**
 * Created by M02323 on 2017/12/11.
 * 使用dataBinding来绑定图片
 */

public class ViewBindAdapter {
	@BindingAdapter("android:src")
	public static void setSrc(ImageView view, Bitmap bitmap) {
		view.setImageBitmap(bitmap);
	}

	@BindingAdapter("android:src")
	public static void setSrc(ImageView view, int resId) {
		view.setImageResource(resId);
	}

	@BindingAdapter({"android:imageUrl", "android:placeHolder", "android:error"})
	public static void loadImage(ImageView imageView, String url, Drawable holderDrawable, Drawable errorDrawable) {
		if (TextUtils.isEmpty(url)) {
			imageView.setVisibility(View.GONE);
		} else {
			imageView.setVisibility(View.VISIBLE);
			Glide.with(imageView.getContext())
					.load(url)
					.placeholder(holderDrawable)
					.error(errorDrawable)
					.into(imageView);
		}
	}
}
